//
//  QDMetroCodeSegmentBar.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/16.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

protocol QDMetroCodeSegmentBarDelegate: class {
    /// 点击进行的回调
    func metroCodeSegmentBar(segmentBar: QDMetroCodeSegmentBar, toIndex: Int)
}



extension QDMetroCodeSegmentBarDelegate {
    func metroCodeSegmentBar(segmentBar: QDMetroCodeSegmentBar, toIndex: Int){ }
}


/// 乘车码上面的segmentBar [乘地铁，坐公交](写死了)
final class QDMetroCodeSegmentBar: UIView {

    /// 地铁按钮
    private let metroButton = UIButton()
    /// 公交按钮
    private let busButton = UIButton()
    /// 代理(废弃回调)
    weak var delegate: QDMetroCodeSegmentBarDelegate?
    /// 用于替代回调的闭包
    @objc var segmentBarClickHandler: ((QDMetroCodeSegmentBar, Int) -> (Void))?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        
        //进行设置
        setContainerButton(button: metroButton, title: "乘地铁")
        setContainerButton(button: busButton, title: "坐公交")
        
        addSubview(metroButton)
        addSubview(busButton)
        
        metroButton.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(busButton)
        }
        
        busButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.width.equalTo(metroButton)
            make.left.equalTo(metroButton.snp.right)
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 内部的响应按钮
    private func setContainerButton(button: UIButton, title: String = ""){
        
        //设置相关属性
        button.titleLabel?.font = Font.regular.font(size: 16)
        button.adjustsImageWhenDisabled = false
        button.adjustsImageWhenHighlighted = false
        
        button.setBackgroundImage(UIColor.white.ritl_image(), for: .selected)
        button.setBackgroundImage(#colorLiteral(red: 0.3921568627, green: 0.7098039216, blue: 0.4549019608, alpha: 1).ritl_image(), for: .normal)

        button.setTitleColor(.white, for: .normal)
        button.setTitleColor(#colorLiteral(red: 0.3921568627, green: 0.7098039216, blue: 0.4549019608, alpha: 1), for: .selected)
        
        button.setTitle(title, for: .normal)
        button.setTitle(title, for: .selected)
        
        button.addTarget(self, action: #selector(containerButtonDidTap(sender:)), for: .touchUpInside)
    }
    
    
    // MARK: Public
    
    /// 选中某个选项
    ///
    /// - Parameters:
    ///   - index: 选中的索引，目前 0 为地铁， 1 为公交
    ///   - action: 是否响应回调
    @objc func selectedIndex(index: Int,action: Bool = false) {
        
        //不进行重复响应
        guard (0...1).contains(index) else { return }//越界
        //重复响应避免
        guard (index == 0 && !metroButton.isSelected) || (index == 1 && !busButton.isSelected) else { return }
        
        //进行选择展示
        metroButton.isSelected = (index == 0)
        busButton.isSelected = (index == 1)
        
        //设置正常
        metroButton.setBackgroundImage(metroButton.isSelected ? UIColor.white.ritl_image() : #colorLiteral(red: 0.3921568627, green: 0.7098039216, blue: 0.4549019608, alpha: 1).ritl_image() , for: .normal)
        busButton.setBackgroundImage(busButton.isSelected ? UIColor.white.ritl_image() : #colorLiteral(red: 0.3921568627, green: 0.7098039216, blue: 0.4549019608, alpha: 1).ritl_image() , for: .normal)
        
        metroButton.setTitleColor(metroButton.isSelected ? #colorLiteral(red: 0.3921568627, green: 0.7098039216, blue: 0.4549019608, alpha: 1) : .white, for: .normal)
        busButton.setTitleColor(busButton.isSelected ? #colorLiteral(red: 0.3921568627, green: 0.7098039216, blue: 0.4549019608, alpha: 1) : .white, for: .normal)
        
        //字体变换
        metroButton.setTitleFont()
        busButton.setTitleFont()
        
        //进行回调
        if action {
            delegate?.metroCodeSegmentBar(segmentBar: self, toIndex: index)
            segmentBarClickHandler?(self,index)
        }
    }
    
    
    /// 按钮的点击
    @objc func containerButtonDidTap(sender: UIButton){
        selectedIndex(index: sender == metroButton ? 0 : 1, action: true)
    }
}


fileprivate extension UIButton {
    func setTitleFont(){
        titleLabel?.font = isSelected.metroSegmentFont
    }
}

fileprivate extension Bool {
    var metroSegmentFont: UIFont {
        return self ? Font.medium.font(size: 16) : Font.regular.font(size: 16)
    }
}
